###############################################################################
# CA cleaning
#
# 11 August 2022
# sbaltz
###############################################################################

import pandas as pd

RAW_FILE_NAME = "../transcribed/humboldt.csv"
SAVE_FILE_NAME = "../ready/humboldt_cleaned.csv"
COUNTY_NAME = "HUMBOLDT"

ct = pd.read_csv(RAW_FILE_NAME)

cleaned = pd.DataFrame()

office = ""
for i in range(len(ct)):
    if "Contest" in str(ct["candidate"][i]):
        office = str(ct["audited"][i]) #Weirdly enough!
    if str(ct["original"][i]) != "nan" and str(ct["audited"][i]) != "nan":
        try:
            state = "CALIFORNIA"
            county = COUNTY_NAME
            candidate = ct["candidate"][i]
            original = int(ct["original"][i])
            audited = int(ct["audited"][i])
            difference = audited - original
            method = "MANUAL"
            if candidate != "Total":
                cleaned = pd.concat([cleaned,
                                     pd.DataFrame([[state,county,office,\
                            candidate,original,audited,difference,method]])],\
                            ignore_index = True)
        except Exception as inst:
            print(inst)

cleaned.columns = ["state","county","office","candidate","original","audited",\
                   "difference","type"]

cleaned.to_csv(SAVE_FILE_NAME, index=False)
